package ioc

import (
	"fmt"
	"github.com/spf13/viper"
	"gopkg.in/gomail.v2"
)

type IEmail interface {
	CreateCheckCodeEmailMessage(code string, email string) error
}
type Email struct {
	viper  *viper.Viper
	Dialer *gomail.Dialer
}

func GoEmailInitialization(viper *viper.Viper) IEmail {
	return &Email{viper: viper,
		Dialer: gomail.NewDialer(
			viper.GetString("email.host"),
			viper.GetInt("email.port"),
			viper.GetString("email.username"),
			viper.GetString("email.password"))}

}
func (e Email) CreateCheckCodeEmailMessage(code string, email string) error {
	message := gomail.NewMessage()
	message.SetHeader("From", e.viper.GetString("email.username"))
	message.SetHeader("To", email)
	message.SetHeader("Cc", code)
	message.SetHeader("Subject", "《来自MooNight的验证邮箱信息！！！》")
	message.SetBody("text/html", fmt.Sprintf("你好，你的邮箱验证码为：<br><h2>%s</h2>", code))
	err := e.Dialer.DialAndSend(message)
	return err
}
